package com.linQing.waterWork.service.sql.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linQing.waterWork.mapper.now.DayquartzMapper;
import com.linQing.waterWork.mapper.now.HourquartzMapper;
import com.linQing.waterWork.mapper.now.MonthquartzMapper;
import com.linQing.waterWork.modle.dto.HourQuartZ.EnergyAnalysisDTO;
import com.linQing.waterWork.modle.entity.Hourquartz;
import com.linQing.waterWork.modle.vo.QueryWaterAndElectricity;
import com.linQing.waterWork.service.sql.HourquartzService;
import com.linQing.waterWork.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
* @author Administrator
* @description 针对表【HourQuartZ】的数据库操作Service实现
* @createDate 2024-04-23 14:32:33
*/
@Service
@Slf4j
public class HourquartzServiceImpl extends ServiceImpl<HourquartzMapper, Hourquartz>
    implements HourquartzService {

//    @Resource
//    private HourquartzMapper hourquartzMapper;

    @Resource
    private DayquartzMapper dayquartzMapper;

//    @Resource
//    private MonthquartzMapper monthquartzMapper;


//    /**
//     * 查询数据库中的每小时用水用电数据并排序返回
//     * @param request 请求参数
//     * @return 每小时用水用电数据并排序
//     */
//    @Override
//    public List<QueryWaterAndElectricity> queryWaterAndElectricOfHour(EnergyAnalysisDTO request) {
//        // 根据参数时间获取到当天开始时间和结束时间
//        String today = DateUtils.parseDateToStr("yyyy-MM-dd", request.getTime());
//        String startTime = today + " 00:00:00";
//        String endTime = today + " 23:59:59";
//        log.debug("当天从{}到{}", startTime, endTime);
//        // 获取当天全部数据
//        List<QueryWaterAndElectricity> queryWaterAndElectricityOfHours =
//                hourquartzMapper.queryWaterAndElectricityByTime(DateUtils.formatD(startTime),
//                                                                DateUtils.formatD(endTime),
//                                                                request.getEquipmentId());
//        // 对数据进行排序
//        Collections.sort(queryWaterAndElectricityOfHours, new Comparator<QueryWaterAndElectricity>() {
//            @Override
//            public int compare(QueryWaterAndElectricity o1, QueryWaterAndElectricity o2) {
//                long l = o1.getUpdateTime().getTime() - o2.getUpdateTime().getTime();
//                if(l > 0) {
//                    return 1;
//                } else if(l < 0) {
//                    return -1;
//                }
//                return 0;
//            }
//        });
//        // 开始对数据进行更细化处理
//        for (QueryWaterAndElectricity queryWaterAndElectricityOfHour : queryWaterAndElectricityOfHours) {
//            int hours = queryWaterAndElectricityOfHour.getUpdateTime().getHours();
//            queryWaterAndElectricityOfHour.setTimeSection(hours);
//        }
//        return queryWaterAndElectricityOfHours;
//    }

    /**
     * 查询数据库中每天用水用电数据并排序返回
     * @param request 请求参数
     * @return 用水用电数据
     */
    @Override
    public List<QueryWaterAndElectricity> queryWaterAndElectricOfDay(EnergyAnalysisDTO request) {
        // 根据参数时间获取到当月开始时间和结束时间
        String todayTimeStr = DateUtils.parseDateToStr("yyyy-MM-dd", new Date()) + " 00:00:00";
        Date todayTimeDate = DateUtils.formatD(todayTimeStr);
        log.debug("今天日期:{}", todayTimeStr);
        Date endTime = new Date(todayTimeDate.getTime()-(1000*60*60*24));
        Date startTime = new Date(endTime.getTime() - (1000*60*60*24*9));
        log.debug("查询时间从{}到{}", startTime, endTime);
        // 获取当天全部数据
        List<QueryWaterAndElectricity> queryWaterAndElectricityOfDays =
                dayquartzMapper.queryWaterAndElectricityByTime(startTime,
                        endTime,
                        request.getEquipmentId());

        if(queryWaterAndElectricityOfDays.size() < 1) {
            return new ArrayList<>();
        }
        // 对数据进行排序
        Collections.sort(queryWaterAndElectricityOfDays, new Comparator<QueryWaterAndElectricity>() {
            @Override
            public int compare(QueryWaterAndElectricity o1, QueryWaterAndElectricity o2) {
                long l = o1.getUpdateTime().getTime() - o2.getUpdateTime().getTime();
                if(l > 0) {
                    return 1;
                } else if(l < 0) {
                    return -1;
                }
                return 0;
            }
        });
        // 开始对数据进行更细化处理
        int i = 0;
        Date date = startTime;
        List<QueryWaterAndElectricity> list = new ArrayList<>();

        for (int j = 0; j < 10; j++) {
            if(i == queryWaterAndElectricityOfDays.size()) {
                QueryWaterAndElectricity queryWaterAndElectricity=  new QueryWaterAndElectricity();
                queryWaterAndElectricity.setUpdateTime(new Date());
                queryWaterAndElectricityOfDays.add(queryWaterAndElectricity);
            }
            if(queryWaterAndElectricityOfDays.get(i).getUpdateTime().getTime() == date.getTime()) {
                log.debug("1下标为: {} 的数据为: {} ", i, queryWaterAndElectricityOfDays.get(i));
                queryWaterAndElectricityOfDays.get(i).setTimeSection(list.size());
                list.add(queryWaterAndElectricityOfDays.get(i));
                i++;
                date = new Date(date.getTime() + (1000*60*60*24));
            } else {
                log.debug("2下标为: {} 的数据为: {} ", i, queryWaterAndElectricityOfDays.get(i));
                QueryWaterAndElectricity queryWaterAndElectricityOfDay = new QueryWaterAndElectricity();
                queryWaterAndElectricityOfDay.setTimeSection(list.size());
                queryWaterAndElectricityOfDay.setEquipmentId(request.getEquipmentId());
                queryWaterAndElectricityOfDay.setEnergyCon(new BigDecimal(0));
                queryWaterAndElectricityOfDay.setFlowCon(new BigDecimal(0));
                queryWaterAndElectricityOfDay.setUpdateTime(date);
                list.add(queryWaterAndElectricityOfDay);
                date = new Date(date.getTime() + (1000*60*60*24));
            }

        }
//        for (QueryWaterAndElectricity queryWaterAndElectricityOfDay : queryWaterAndElectricityOfDays) {
//
//            queryWaterAndElectricityOfDay.setTimeSection(i++);
//        }
        return list;
    }

//    /**
//     * 查询数据库中每月用水用电数据并排序返回
//     * @param request 请求参数
//     * @return 用水用电数据
//     */
//    @Override
//    public List<QueryWaterAndElectricity> queryWaterAndElectricOfMonth(EnergyAnalysisDTO request) {
//        // 根据参数时间获取到当天开始时间和结束时间
//        String thisYear = DateUtils.parseDateToStr("yyyy", request.getTime());
//        String startTime = thisYear + "-01-01 00:00:00";
//        String endTime = thisYear + "-12-31 23:59:59";
//        log.debug("当年从{}到{}", startTime, endTime);
//        // 获取当天全部数据
//        List<QueryWaterAndElectricity> queryWaterAndElectricityOfMonths =
//                monthquartzMapper.queryWaterAndElectricityByTime(DateUtils.formatD(startTime),
//                        DateUtils.formatD(endTime),
//                        request.getEquipmentId());
//        // 对数据进行排序
//        Collections.sort(queryWaterAndElectricityOfMonths, new Comparator<QueryWaterAndElectricity>() {
//            @Override
//            public int compare(QueryWaterAndElectricity o1, QueryWaterAndElectricity o2) {
//                long l = o1.getUpdateTime().getTime() - o2.getUpdateTime().getTime();
//                if(l > 0) {
//                    return 1;
//                } else if(l < 0) {
//                    return -1;
//                }
//                return 0;
//            }
//        });
//        // 开始对数据进行更细化处理
//        for (QueryWaterAndElectricity queryWaterAndElectricityOfMonth : queryWaterAndElectricityOfMonths) {
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(queryWaterAndElectricityOfMonth.getUpdateTime());
//            int month = calendar.get(Calendar.MONTH) + 1;
//            queryWaterAndElectricityOfMonth.setTimeSection(month);
//        }
//        return queryWaterAndElectricityOfMonths;
//    }

}




